package com.freelancer.musicinstruction.activity;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Looper;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.freelancer.musicinstruction.R;
import com.freelancer.musicinstruction.activity.delegates.ThreadManager;

public class YoutubeVideoPage extends Activity {
	
	private static final String TAG = YoutubeVideoPage.class.getSimpleName();
	
	
	Bundle details;
	TextView videoTitleLabel;
	VideoView videoPlayer;
	
	String videoTitle;
	String videoLink;
	
	VideoView videoView;
	SurfaceHolder videoHolder;
	
	MediaPlayer player;
	ThreadManager threadManager;
	
	private static int percent = 0;
	
	
	@Override
    public void onCreate(Bundle savedInstanceState){
		
		super.onCreate(savedInstanceState);
		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		
        setContentView(R.layout.youtube_watch_video );
        
        this.details = getIntent().getExtras();
        
        videoTitle = details.getString( "video_title" );
        videoLink = details.getString( "video_url" );
        
        
        videoView = (VideoView) findViewById( R.id.video_player );
        
        videoTitleLabel = (TextView)findViewById( R.id.video_title);
        
        videoTitleLabel.setText( videoTitle );
        
        //videoPlayer = (VideoView)findViewById( R.id.video_player );
        videoHolder = videoView.getHolder();
        
        try{
        	
        	
	        
	        
	        //player.reset();
	        //player
	        
	        //player.setDataSource( "http://www2.i-u.de/user/charlesa/video.mp4" ); // audio but no video here
	        //player.setDataSource( "http://www2.i-u.de/user/charlesa/unforgiven.mp3" ); // mp3 works fine. some playback glitches. nothing horrible
	        
	        //PVMFFailure (H.263 video. no error but nothing showing up. no audio, no video ) This one took a while before spitting out error. seemed player liked it more than mpeg-4
	        //This return code is for general failure. Please refer API documentation for possible cause of
	        //failures. Please note that its value is also (-1)
	        //player.setDataSource( "rtsp://rtsp2.youtube.com/CiILENy73wIaGQkCZRGoEt5XyxMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"); // H.263 video. no error but nothing showing up. no audio, no video. keeps calling onbuffer but no progress. always buffer(0)
	        
	        //PVMFFailure (MPEG-4 video. no error but nothing showing up. no audio, no video )
	        //This return code is for general failure. Please refer API documentation for possible cause of
	        //failures. Please note that its value is also (-1)
	        //player.setDataSource( "rtsp://rtsp2.youtube.com/CiILENy73wIaGQkCZRGoEt5XyxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp");
	        
	        //PVMFErrCorrupt (flash video )
	        //This error occurs due to data corruption being detected. A typical example is when an invalid
	        //media stream is encountered in datapath.
	        //player.setDataSource( "http://www2.i-u.de/user/charlesa/video.flv" );
	        
	        videoView.setOnPreparedListener(new OnPreparedListener (){

				@Override
				public void onPrepared(MediaPlayer media_player) {
					
					runOnUiThread( new Runnable( ){

						@Override
						public void run() {
							Toast.makeText( YoutubeVideoPage.this , "media source ready for playback", Toast.LENGTH_SHORT).show();
						}
						
					});
					media_player.start();
				}
	        	
	        });
	        
	        
	        /**
	         * happens often. so display the toast in a different thread.
	         */
	        /*videoView.setOnBufferingUpdateListener( new OnBufferingUpdateListener (){

				@Override
				public void onBufferingUpdate(MediaPlayer mp, int percent) {
					
					if( percent > YoutubeVideoPage.percent ){
						
						final StringBuffer sb = new StringBuffer();
						sb.append( "buffer: "+percent );
						sb.append( "% ready");

						Toast.makeText( YoutubeVideoPage.this , sb.toString(), Toast.LENGTH_SHORT).show();

						YoutubeVideoPage.percent+=10;
					}
					
				}
	        	
	        });*/
	        
	        videoView.setOnCompletionListener( new OnCompletionListener (){

				@Override
				public void onCompletion(MediaPlayer player) {
					runOnUiThread( new Runnable( ){

						@Override
						public void run() {
							Toast.makeText( YoutubeVideoPage.this , "video playback complete", Toast.LENGTH_SHORT).show();
						}
						
					});
					player.release();
					YoutubeVideoPage.this.onBackPressed();
				}
	        	
	        });
	        
	        videoView.setOnErrorListener( new OnErrorListener () {

				@Override
				public boolean onError(MediaPlayer mp, int what, int extra) {
					
					final StringBuilder sb = new StringBuilder();
					sb.append( "problem: ");
					sb.append(  what == 1 ?  "unknown error" : "server died" );
					
					runOnUiThread( new Runnable( ){

						@Override
						public void run() {
							Toast.makeText( YoutubeVideoPage.this , sb.toString(), Toast.LENGTH_SHORT).show();
						}
						
					});
					
					
					return true;
				}
	        	
	        });
	        
	       /* videoView.setOnInfoListener( new OnInfoListener() {

				@Override
				public boolean onInfo(MediaPlayer player, int what, int extra) {
					
					final StringBuilder sb = new StringBuilder();
					sb.append( "info: ");
					
					switch( what ){
						
						case 1:
							sb.append( "unknown" );
							break;
						case 800:
							sb.append( "bad interleaving" );
							break;
						case 801:
							sb.append( "live stream. cannot be seeked" );
							break;
						case 802:
							sb.append( "metadata update");
							break;
						case 700:
							sb.append( "video too complex for decoder. you might hear only audio from here" );
							break;
						default:
							sb.append( "unknown");
							
					}
					
					runOnUiThread( new Runnable( ){

						@Override
						public void run() {
							Toast.makeText( YoutubeVideoPage.this , sb.toString(), Toast.LENGTH_SHORT).show();
						}
						
					});
					
					return true;
				}
	        	
	        });*/
	        
	        
	        
	        
	        //This stuff below video.mp4 works but no video only audio
	        
	        //player.setDataSource( videoLink );
	        //player.setDisplay( videoHolder );
			//player.prepareAsync();
			//player.start();
	        
	        //videoView.setVideoURI( Uri.parse( "http://www2.i-u.de/user/charlesa/video.mp4" ));
	        //videoView.setVideoURI( Uri.parse( "rtsp://rtsp2.youtube.com/CiILENy73wIaGQkCZRGoEt5XyxMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp" ));
	        videoView.setVideoURI( Uri.parse( "http://www2.i-u.de/user/charlesa/unforgiven.mp3") );
        	MediaController controller = new MediaController( this );
        	controller.setMediaPlayer( videoView );
        	videoView.setMediaController( controller );
        	
        	videoView.requestFocus();
        	controller.show( 10000 );
        	controller.setVisibility( View.VISIBLE );
        	controller.setAnchorView( videoView );
        	controller.setEnabled( true );
        	controller.bringToFront();
        	
        }
        catch(Exception e ){
        	Log.d(TAG, e.getMessage());
        }
 
	}
}
